<?php
/**
 * Clase Base FN para BaseSoDeliveryLineaFN
 *
 * @package    
 * @subpackage BaseSoDeliveryLineaFN
 * @author     hans
 * @version    SVN:: BaseSoDeliveryLineaFN.class.php,v 1.1 2012-05-01 22:33:49 Hans.parra Exp $
 */

require_once('class/im/dlv/BN/SoDeliveryLineaBN.class.php');

class BaseSoDeliveryLineaFN extends SoDeliveryLineaBN
{
  private static $fieldNames = array (
    'DlvId'=>'dlvl_dlvId', 'LineaId'=>'dlvl_lineaId', 'ArtId'=>'dlvl_artId', 'ArtNombre'=>'dlvl_artNombre', 'AlmId'=>'dlvl_almId', 'Cant'=>'dlvl_cant', 'UoM'=>'dlvl_UoM', 'Costo'=>'dlvl_costo', 'Obs'=>'dlvl_obs', 'CreaUsr'=>'dlvl_creaUsr', 'CreaFecha'=>'dlvl_creaFecha', 'ModifUsr'=>'dlvl_modifUsr', 'ModifFecha'=>'dlvl_modifFecha'  
  );

  public function select()
  {
    try
    {
      /*$tamPag = !property_exists($this, 'tamPag')?9999:$this->tamPag;
      $pagina = !property_exists($this, 'page')||$this->page==""?1:$this->page;
      $numeroRegistros = $this->selectNumRows();
      $limitInf = ($pagina-1)*$tamPag;*/

      $where = $this->buildConsult();
      //$orden = $this->buildOrderByField();

      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      //$sql = "SELECT dlvl_dlvId, dlvl_lineaId, dlvl_artId, dlvl_artNombre, dlvl_almId, dlvl_cant, dlvl_UoM, dlvl_costo, dlvl_obs, dlvl_creaUsr, dlvl_creaFecha, dlvl_modifUsr, dlvl_modifFecha FROM so_delivery_linea WHERE 1 ".$where." ".$orden."  LIMIT ".$limitInf.",".$tamPag;
      $sql = "SELECT dlvl_dlvId, dlvl_lineaId, dlvl_artId, dlvl_artNombre, dlvl_almId, dlvl_cant, dlvl_UoM, dlvl_costo, dlvl_obs, dlvl_creaUsr, dlvl_creaFecha, dlvl_modifUsr, dlvl_modifFecha FROM so_delivery_linea WHERE 1 ".$where;
        $result = mysql_query($sql);
      $num_rows = mysql_num_rows($result);
      $a_tmp = array();
      if($num_rows>0){
        while ($o_valor = mysql_fetch_object($result)) {
          $a_tmp[] = $this->insertObjectBN($o_valor);
        }
      }
      
      return count($a_tmp)>0?$a_tmp:null;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function selectNumRows()
  {
    try
    {
      $where = $this->buildConsult();

      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      $sql = "SELECT dlvl_dlvId, dlvl_lineaId, dlvl_artId, dlvl_artNombre, dlvl_almId, dlvl_cant, dlvl_UoM, dlvl_costo, dlvl_obs, dlvl_creaUsr, dlvl_creaFecha, dlvl_modifUsr, dlvl_modifFecha FROM so_delivery_linea WHERE 1 ".$where;
        $result = mysql_query($sql);
      $num_rows = mysql_num_rows($result);
      
      return $num_rows;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function buildConsult()
  {
    try
    {
      $where = "";

      if($this->getDlvId()){ $where .= " AND dlvl_dlvid = '".$this->getDlvId()."'"; }
      if($this->getLineaId()){ $where .= " AND dlvl_lineaid = '".$this->getLineaId()."'"; }
      if($this->getArtId()){ $where .= " AND dlvl_artid = '".$this->getArtId()."'"; }
      if($this->getArtNombre()){ $where .= " AND dlvl_artnombre = '".$this->getArtNombre()."'"; }
      if($this->getAlmId()){ $where .= " AND dlvl_almid = '".$this->getAlmId()."'"; }
      if($this->getCant()){ $where .= " AND dlvl_cant = '".$this->getCant()."'"; }
      if($this->getUoM()){ $where .= " AND dlvl_uom = '".$this->getUoM()."'"; }
      if($this->getCosto()){ $where .= " AND dlvl_costo = '".$this->getCosto()."'"; }
      if($this->getObs()){ $where .= " AND dlvl_obs = '".$this->getObs()."'"; }
      if($this->getCreaUsr()){ $where .= " AND dlvl_creausr = '".$this->getCreaUsr()."'"; }
      if($this->getCreaFecha()){ $where .= " AND dlvl_creafecha = '".$this->getCreaFecha()."'"; }
      if($this->getModifUsr()){ $where .= " AND dlvl_modifusr = '".$this->getModifUsr()."'"; }
      if($this->getModifFecha()){ $where .= " AND dlvl_modiffecha = '".$this->getModifFecha()."'"; }
      return $where;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function buildOrderByField()
  {
    try
    {
      $order_by = "";

      if(property_exists($this,'sortCampos')){
      if($this->sortCampos != ""){
        $a_valCampos = explode(";", $this->sortCampos);
        $a_valtype   = explode(";", $this->sortType);
        $order_by = " ORDER BY ";
        $a_order = array();
        foreach ($a_valCampos as $key => $val){
          $sTmpOrder = "";
          if(array_key_exists($val, self::$fieldNames)){
            $sTmpOrder .= self::$fieldNames[$val];
            switch ($a_valtype[$key]){
              case 1: $sTmpOrder .= " ASC"; break;
              case 2: $sTmpOrder .= " DESC"; break;
            }
            $a_order[] = $sTmpOrder;
          }
        }
        $order_by .= implode(", ", $a_order);
      }
      }
      return $order_by;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* B�squeda del registro por la llave primaria */
  public function selectByPK()
  {
    try
    {
      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      $sql = "SELECT dlvl_dlvId, dlvl_lineaId, dlvl_artId, dlvl_artNombre, dlvl_almId, dlvl_cant, dlvl_UoM, dlvl_costo, dlvl_obs, dlvl_creaUsr, dlvl_creaFecha, dlvl_modifUsr, dlvl_modifFecha FROM so_delivery_linea WHERE 1 AND dlvl_dlvId='".$this->getDlvId()."' AND dlvl_lineaId='".$this->getLineaId()."'; ";
        $result = mysql_query($sql);
      $o_Propel = $result ? mysql_fetch_object($result) : null;
      

      return $o_Propel?$this->insertObjectBN($o_Propel):null;

    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* Devuelve un objeto con m�todos de acceso */
  private function insertObjectBN($o_propel){
    $o_BaseSoDeliveryLineaFN = new SoDeliveryLineaBN();
    $o_BaseSoDeliveryLineaFN->setDlvId($o_propel->dlvl_dlvId);
    $o_BaseSoDeliveryLineaFN->setLineaId($o_propel->dlvl_lineaId);
    $o_BaseSoDeliveryLineaFN->setArtId($o_propel->dlvl_artId);
    $o_BaseSoDeliveryLineaFN->setArtNombre($o_propel->dlvl_artNombre);
    $o_BaseSoDeliveryLineaFN->setAlmId($o_propel->dlvl_almId);
    $o_BaseSoDeliveryLineaFN->setCant($o_propel->dlvl_cant);
    $o_BaseSoDeliveryLineaFN->setUoM($o_propel->dlvl_UoM);
    $o_BaseSoDeliveryLineaFN->setCosto($o_propel->dlvl_costo);
    $o_BaseSoDeliveryLineaFN->setObs($o_propel->dlvl_obs);
    $o_BaseSoDeliveryLineaFN->setCreaUsr($o_propel->dlvl_creaUsr);
    $o_BaseSoDeliveryLineaFN->setCreaFecha($o_propel->dlvl_creaFecha);
    $o_BaseSoDeliveryLineaFN->setModifUsr($o_propel->dlvl_modifUsr);
    $o_BaseSoDeliveryLineaFN->setModifFecha($o_propel->dlvl_modifFecha);
    return $o_BaseSoDeliveryLineaFN;
  }
}